package com.zp.business.impl.diagnosis;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.api.diagnosis.HisDiagnosis;
import com.zp.business.api.diagnosis.HisDiagnosisBo;
import com.zp.business.api.diagnosis.HisDiagnosisVo;
import com.zp.business.entity.patient.bo.ClinicMasterBo;
import com.zp.business.mapper.diagnosis.HisDiagnosisMapper;
import com.zp.business.service.diagnosis.IHisDiagnosisService;
import com.zp.business.service.patient.IClinicMasterService;
import com.zp.common.core.constant.BaseConstants;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 患者诊断表Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-01-01
 */
@RequiredArgsConstructor
@Service
public class HisDiagnosisServiceImpl implements IHisDiagnosisService {

    private final HisDiagnosisMapper baseMapper;

    private final IClinicMasterService clinicMasterService;

    /**
     * 查询患者诊断表
     */
    @Override
    public HisDiagnosisVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询患者诊断表列表(分页)
     */
    @Override
    public TableDataInfo<HisDiagnosisVo> queryPageList (HisDiagnosisBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisDiagnosis> lqw = buildQueryWrapper(bo);
        Page<HisDiagnosisVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询患者诊断表列表(不分页)
     */
    @Override
    public List<HisDiagnosisVo> queryList (HisDiagnosisBo bo) {
        LambdaQueryWrapper<HisDiagnosis> lqw = buildQueryWrapper(bo);
        lqw.orderByAsc(HisDiagnosis::getSort);
        List<HisDiagnosisVo> hisDiagnosisVos = baseMapper.selectVoList(lqw);
        hisDiagnosisVos.forEach(item -> {
            item.setIsEdit(false);
        });
        return hisDiagnosisVos;
    }

    private LambdaQueryWrapper<HisDiagnosis> buildQueryWrapper (HisDiagnosisBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisDiagnosis> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getParentIds()), HisDiagnosis::getParentIds, bo.getParentIds());
        lqw.eq(StringUtils.isNotBlank(bo.getParentId()), HisDiagnosis::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), HisDiagnosis::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagnosisType()), HisDiagnosis::getDiagnosisType, bo.getDiagnosisType());
        lqw.like(StringUtils.isNotBlank(bo.getIcdName()), HisDiagnosis::getIcdName, bo.getIcdName());
        lqw.eq(StringUtils.isNotBlank(bo.getIcdCode()), HisDiagnosis::getIcdCode, bo.getIcdCode());
        lqw.eq(StringUtils.isNotBlank(bo.getOperation()), HisDiagnosis::getOperation, bo.getOperation());
        lqw.eq(StringUtils.isNotBlank(bo.getEffect()), HisDiagnosis::getEffect, bo.getEffect());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), HisDiagnosis::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getBasis()), HisDiagnosis::getBasis, bo.getBasis());
        lqw.eq(StringUtils.isNotBlank(bo.getInoroutFlag()), HisDiagnosis::getInoroutFlag, bo.getInoroutFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getClinicId()), HisDiagnosis::getClinicId, bo.getClinicId());
        lqw.eq(StringUtils.isNotBlank(bo.getSituation()), HisDiagnosis::getSituation, bo.getSituation());
        lqw.eq(StringUtils.isNotBlank(bo.getVisitId()), HisDiagnosis::getVisitId, bo.getVisitId());
        lqw.eq(StringUtils.isNotBlank(bo.getDiagnosisDoctor()), HisDiagnosis::getDiagnosisDoctor, bo.getDiagnosisDoctor());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), HisDiagnosis::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), HisDiagnosis::getRemarks, bo.getRemarks());
        lqw.eq(StringUtils.isNotBlank(bo.getPathologyNo()), HisDiagnosis::getPathologyNo, bo.getPathologyNo());
        lqw.eq(StringUtils.isNotBlank(bo.getPathologicDiagnosis()), HisDiagnosis::getPathologicDiagnosis, bo.getPathologicDiagnosis());
        lqw.eq(StringUtils.isNotBlank(bo.getPatientId()), HisDiagnosis::getPatientId, bo.getPatientId());
        lqw.eq(bo.getSort() != null, HisDiagnosis::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增患者诊断表
     */
    @Override
    @Transactional
    public Boolean insertByBo (HisDiagnosisBo bo) {
        HisDiagnosis add = BeanUtil.toBean(bo, HisDiagnosis.class);
        validEntityBeforeSave(add);
        add.initEntity();
        add.setDiagnosisDoctor(LoginHelper.getLoginUser().getNickName());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            flag = updateClinicType(bo);
        }
        return flag;
    }

    @Transactional
    public boolean updateClinicType (HisDiagnosisBo bo) {
        ClinicMasterBo clinicMasterBo = new ClinicMasterBo();
        clinicMasterBo.setId(bo.getClinicId());
        clinicMasterBo.setClinicType("2");
        boolean b = clinicMasterService.updateClinicType(clinicMasterBo);
        if (b) {
            //修改接诊医生doctor_id
            clinicMasterService.updateDoctorId(bo.getClinicId(), bo.getIsUpdateDoctorId());
        }
        return b;
    }

    /**
     * 修改患者诊断表
     */
    @Override
    @Transactional
    public Boolean updateByBo (HisDiagnosisBo bo) {
        HisDiagnosis update = BeanUtil.toBean(bo, HisDiagnosis.class);
        update.setUpdateBy(LoginHelper.getLoginUser().getNickName());
        update.setUpdateTime(new Date());
        validEntityBeforeSave(update);
        boolean b = baseMapper.updateById(update) > 0;
        if (StringUtils.equals(bo.getInoroutFlag(), BaseConstants.IS_CLINIC)) {
            updateClinicType(bo);
        }
        return b;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (HisDiagnosis entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除患者诊断表
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
